// Application.cpp: implementation of the Application class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Application.h"
#include "Configration.h"
#include "Project.h"

#include "ProjectTemplateManager.h"
#include "ProjectTemplate.h"
#include "DomainFrameRes.h"
#include "ResourceInstance.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

namespace PDMS
{
	namespace PDMSFrame
	{
//		IMPLEMENT_SINGLETON( Application );

		Application* Application::Instance() {
			static Application app;

		//	app.ActiveConfigration( app.ActiveProject()->GetCurConfigration() );
			return &app;
		}
		
		Application::Application()
		{
			m_pActivePrj = NULL;
			m_pActiveConfig = NULL;
			m_strTemplPath = _T("D:\\");
		}
		
		Application::~Application()
		{
			if ( m_pActivePrj != NULL )
				delete m_pActivePrj;
		}
		
		
		Project* Application::ActiveProject() 
		{
			return m_pActivePrj;
		}
		
		
		const UeObjectId& Application::AcitveObject() 
		{
			if ( m_ActiveObjId.IsNull() )
			{
				UeDbObject* pObj = NULL;
				ASSERT( ActiveProject() != NULL );
				UESoft::ErrorStatus es = ActiveProject()->GetCurDatabase()
										 ->TreeModel( pObj, UeDb::eForRead );
				if ( es == UESoft::eOk )
				{
					m_ActiveObjId = pObj->Id();
					pObj->Close();
				}
			}
			return m_ActiveObjId;
		}

		UESoft::ErrorStatus Application::NewProject( const StdString& strPath,
			                                         const StdString& strName,
													 const StdString& strTemplate )
		{
			CResourceInstance rs;

			//Create Project
			ProjectTemplate* pTemplate = NULL;
			pTemplate = ProjectTemplateManager::Instance()
				                   .GetTemplate(strTemplate);
			ASSERT(pTemplate);
			
			UESoft::ErrorStatus es = 
				pTemplate->CreateProject(strPath,strName);
			if(es != UESoft::eOk)
			{
				StdString strTemp = "";
				strTemp.LoadString(IDC_WARRING_FAILCREATEPROJECT);
				AfxMessageBox(strTemp);
				return es;
			}


			//Read Project
			Project* pProject = new Project;
			CString strFilePath = strPath;

			if( strPath.GetAt(strPath.GetLength()-1) != '\\' )
			{
				strFilePath += "\\" + strName + "\\" +strName + ".ueprj";
			}
			else
			{
				strFilePath += strName + "\\" + strName + ".ueprj";
			}
			es = pProject->ReadPrjDatabase(strFilePath);
			if( es != UESoft::eOk )
			{
				StdString strTemp;
				strTemp.LoadString(IDC_WARRING_FAILREAD);
				AfxMessageBox(strTemp);
				delete pProject;
				return es;
			}
			m_pActivePrj = pProject;
			Application::Instance()->ActiveConfigration(Application::Instance()
				->ActiveProject()->GetCurConfigration());
			return UESoft::eOk;
		}
		
		UESoft::ErrorStatus Application::OpenProject( const StdString& strPath )
		{
			CResourceInstance rs;

			Project* pProject = new Project;
			UESoft::ErrorStatus es;

			es = pProject->ReadPrjDatabase(strPath);
			if( es != UESoft::eOk )
			{
				StdString strTemp;
				strTemp.LoadString(IDC_WARRING_FAILREAD);
				AfxMessageBox(strTemp);
				delete pProject;
				return es;
			}
			m_pActivePrj = pProject;
            Application::Instance()->ActiveConfigration(Application::Instance()
				->ActiveProject()->GetCurConfigration());
			
			return UESoft::eOk;
			
		}
		
		void Application::CloseProject()
		{
			delete m_pActivePrj;
			m_pActivePrj = NULL;

		}
		
		UESoft::ErrorStatus Application::ExecuteConfigration()
		{
			return UESoft::eOk;
		}
		
		UESoft::ErrorStatus Application::InitApplication()
		{
			return UESoft::eOk;
		}
		
		UESoft::ErrorStatus Application::UnloadApplication()
		{
			return UESoft::eOk;
		}
	}
}
